package com.sinosoft.orderinfomodel;

import java.math.BigDecimal;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sinosoft.base.GenericManagerImpl;

@Service
public class OrderInfoModelManagerImp extends GenericManagerImpl<OrderInfoModel, Long> implements OrderInfoModelManager {

	OrderInfoModelDao oDao;

    @Autowired
    public OrderInfoModelManagerImp(OrderInfoModelDao oDao) {
        super(oDao);
        this.oDao = oDao;
    }

	public List<OrderInfoModel> getOrderInfoModelList(OrderInfoModel orderinfomodel) {
		String username = orderinfomodel.getUserid();
		BigDecimal id = orderinfomodel.getOrdermodelid();
		BigDecimal priority = orderinfomodel.getPriority();
		String satelliteid = orderinfomodel.getSatelliteid();
		String starttime = orderinfomodel.getStarttime();
		String endtime = orderinfomodel.getEndtime();
		String sensorid = orderinfomodel.getSensorid();
		StringBuffer hql = new StringBuffer("from OrderInfoModel where 1=1");
		if(satelliteid!=null && !satelliteid.isEmpty())
		{
			hql.append(" and (userid='"+username+"'");
			hql.append(" or isdefaulmodel=3)");
		}
		hql.append(id!=null && !id.equals("")?" and id="+id:"");
		hql.append(priority!=null && !priority.equals("")?" and priority="+priority:"");
		hql.append(satelliteid!=null && !satelliteid.isEmpty()?" and satelliteid='"+satelliteid+"'":"");
		hql.append(starttime!=null && !starttime.isEmpty()?" and createtime>to_date('"+starttime+"','yyyy-MM-dd HH24:mi:ss')":"");
		hql.append(endtime!=null && !endtime.isEmpty()?" and createtime<to_date('"+endtime+"','yyyy-MM-dd HH24:mi:ss')":"");
		hql.append(sensorid!=null && !sensorid.isEmpty()?" and sensorid='"+sensorid+"'":"");
		return oDao.getOrderInfoModelList(hql.toString());
	}

	public OrderInfoModel saveOrderInfoModel(OrderInfoModel o)
	{
		return oDao.saveOrderInfoModel(o);
	}
	
	public OrderInfoModel getOrderInfoModelById(String id)
	{
		return oDao.getOrderInfoModelById(id);
		
	}

	public void removeOrderInfo(OrderInfoModel orderinfo) {
		oDao.removeOrderInfo(orderinfo);
	}
	
	public OrderInfoModel getOrderInfoModelByDefaultAndUsername(String username,int isdefault)
	{
		return oDao.getOrderInfoModelByDefaultAndUsername(username, isdefault);
	}
	
	public OrderInfoModel getSystemOrderInfoModel()
	{
		return oDao.getSystemOrderInfoModel();
	}
	
	public int getOrderInfoModelCount(OrderInfoModel orderinfomodel)
	{
		String username = orderinfomodel.getUserid();
		BigDecimal id = orderinfomodel.getOrdermodelid();
		BigDecimal priority = orderinfomodel.getPriority();
		String satelliteid = orderinfomodel.getSatelliteid();
		String starttime = orderinfomodel.getStarttime();
		String endtime = orderinfomodel.getEndtime();
		String sensorid = orderinfomodel.getSensorid();
		String userid = orderinfomodel.getUserid();
		StringBuffer hql = new StringBuffer("select count(*) from zywx_Order_Info_Model where 1=1");
		if(satelliteid!=null && !satelliteid.isEmpty())
		{
			hql.append(" and (userid='"+username+"'");
			hql.append(" or isdefaulmodel=3)");
		}
		hql.append(id!=null && !id.equals("")?" and ordermodelid="+id:"");
		hql.append(priority!=null && !priority.equals("")?" and priority="+priority:"");
		hql.append(satelliteid!=null && !satelliteid.isEmpty()?" and satelliteid='"+satelliteid+"'":"");
		hql.append(starttime!=null && !starttime.isEmpty()?" and createtime>to_date('"+starttime+"','yyyy-MM-dd HH24:mi:ss')":"");
		hql.append(endtime!=null && !endtime.isEmpty()?" and createtime<to_date('"+endtime+"','yyyy-MM-dd HH24:mi:ss')":"");
		hql.append(sensorid!=null && !sensorid.isEmpty()?" and sensorid='"+sensorid+"'":"");
		hql.append(userid!=null && !userid.isEmpty()?" and userid='"+userid+"'":"");
		return oDao.getOrderInfoModelCount(hql.toString());
	}
	
	public List<OrderInfoModel> getOrderInfoModelList(OrderInfoModel orderinfo,int first,int pagesize)
	{
		return oDao.getOrderInfoModelList(orderinfo, first, pagesize);
	}
	
	public OrderInfoModel getDefaultOrderInfoModel(String username)
	{
		final int userdefault = 2;
		OrderInfoModel oimodel = oDao.getOrderInfoModelByDefaultAndUsername(username, userdefault);
		if(oimodel == null)
			oimodel = oDao.getOrderInfoModelBySystem();
		return oimodel;
	}
	
	/**
	 * 查询订单模板
	 * @param sql
	 * @return
	 */
	public List<OrderInfoModel> getOrderInfoModel(String productlevel,String satelliteid,String sensorid)
	{
		StringBuffer hql = new StringBuffer("from OrderInfoModel where productlevel='");
		hql.append(productlevel+"'");
		hql.append(" and satelliteid='"+satelliteid+"'");
		hql.append(" and sensorid='"+sensorid+"'");
		return oDao.getOrderInfoModel(hql.toString());
	}
	
	/**
	 * 修改订单模板
	 * @param o
	 * @return
	 */
	public OrderInfoModel updateInfoModel(OrderInfoModel o)
	{
		return oDao.updateInfoModel(o);
	}
	
	/**
	 * 通过产品级别，卫星，传感器查询订单模板
	 */
	public OrderInfoModel getSystemOrderInfoModel(String productlevel,String satelliteid,String sensorid)
	{
		return oDao.getSystemOrderInfoModel(productlevel, satelliteid, sensorid);
	}
	
	public OrderInfoModel saveOrderInfoModelExistDefaultModel(OrderInfoModel o,String username, int defa)
	{
		return oDao.saveOrderInfoModelExistDefaultModel(o, username, defa);
	}
	
}
